﻿using Google.GData.Calendar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PW_kalendarz
{
    /// <summary>
    /// Interaction logic for General.xaml
    /// </summary>
    public partial class General : Window
    {

        static CalendarService credentials;

        public General()
        {
            var loginWindow = new MainWindow(this);
            this.Hide();
            loginWindow.Show();
            InitializeComponent();

        }

        public static void setCredentials(CalendarService c)
        {
            credentials = c;
        }

        private void loginButton_Copy_Click(object sender, RoutedEventArgs e)
        {

        }

        private string RandomString(int size)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }

            return builder.ToString();
        }

        private void newEventButtonClick(object sender, RoutedEventArgs e)
        {
            randomId.Content = RandomString(6);
            LinkedList<string> usersEmails = new LinkedList<string>();
            if (email1.Text != "") usersEmails.AddLast(email1.Text);
            if (email2.Text != "") usersEmails.AddLast(email2.Text);
            if (email3.Text != "") usersEmails.AddLast(email3.Text);
            if (email4.Text != "") usersEmails.AddLast(email4.Text);
            if (email5.Text != "") usersEmails.AddLast(email5.Text);
            foreach (String s in usersEmails)
            {
                CalendarHelper.addUser(s);
                Mailer.sendMail(s, "Powiadomienie o spotkaniu", "Zaloguj się w aplikacji PW-kalendarz i zadeklaruj swoją dostępność dla wydarzenia " + randomId.Content + ".");
            }
        }

        private void declareAvClick(object sender, RoutedEventArgs e)
        {
            String newEvent = eventId.Text;

            if (newEvent == "") newEvent = "DEFAULT";

            if (date1.SelectedDate != null)
            {
                DateTime eStart1 = date1.SelectedDate.Value.Date;
                eStart1 += new TimeSpan(Convert.ToInt32(start1.Text), 0, 0);
                DateTime eEnd1 = date1.SelectedDate.Value.Date;
                eEnd1 += new TimeSpan(Convert.ToInt32(end1.Text), 0, 0);
                CalendarHelper.addEntry(newEvent, eStart1, eEnd1, credentials);
            }

            if (date2.SelectedDate != null)
            {
                DateTime eStart2 = date2.SelectedDate.Value.Date;
                eStart2 += new TimeSpan(Convert.ToInt32(start2.Text), 0, 0);
                DateTime eEnd2 = date2.SelectedDate.Value.Date;
                eEnd2 += new TimeSpan(Convert.ToInt32(end2.Text), 0, 0);
                CalendarHelper.addEntry(newEvent, eStart2, eEnd2, credentials);
            }
            if (date3.SelectedDate != null)
            {
                DateTime eStart3 = date3.SelectedDate.Value.Date;
                eStart3 += new TimeSpan(Convert.ToInt32(start3.Text), 0, 0);
                DateTime eEnd3 = date3.SelectedDate.Value.Date;
                eEnd3 += new TimeSpan(Convert.ToInt32(end3.Text), 0, 0);
                CalendarHelper.addEntry(newEvent, eStart3, eEnd3, credentials);
            }
        }

        private void getEventsClick(object sender, RoutedEventArgs e)
        {
            Solver s = new Solver();
            foreach (CustomEvent ev in CalendarHelper.getEntries(eventIdEnd.Text))
            {
                s.addEvent(ev);
            }
            CustomEventWithRate x = s.solve();

            MessageBoxResult result = MessageBox.Show("Spotkanie ustalone na dzień " + x.Date + ", przedział godzinowy: " + x.StartHour/10 + " - " + x.EndHour/10 + ". Termin pasuje: " + x.rate);

        }

    }
}
